<?php
error_reporting(E_ALL ^ E_NOTICE);
set_time_limit(0);
$db_name="tlck";  
 
$table_name="tlck";  
 
$connection= @mysql_connect("localhost","root","root") or die(mysql_error());  
 
$db= @mysql_select_db($db_name,$connection) or die (mysql_error());  
 
$logdate =  mysql_query("SELECT DISTINCT date FROM `tlck`");
$dateTime = date("Y-m-d",strtotime("-1 day"));

    //$deviceid = mysql_fetch_array($deviceid_query);
    //while($deviceid[deviceid])

while($everylogdate = mysql_fetch_array($logdate))
{
var_dump($everylogdate[date]);
  $deviceid_query = mysql_query("SELECT DISTINCT a.deviceid
        FROM (

        SELECT DISTINCT a.deviceid, a.date
        FROM tlck a
        WHERE a.jailbreak = '0' and a.date
        BETWEEN DATE_SUB( '$everylogdate[date]', INTERVAL 14
        DAY )
        AND DATE_SUB( '$everylogdate[date]', INTERVAL 7
        DAY )
        GROUP BY a.deviceid
        HAVING count( 1 ) >1
        )a
        LEFT JOIN (

        SELECT DISTINCT deviceid
        FROM tlck
        WHERE jailbreak = '0' and date = DATE_SUB( '$everylogdate[date]', INTERVAL 6
        DAY )
        )b ON a.deviceid = b.deviceid
        WHERE b.deviceid IS NOT NULL
 ");


  while($deviceid = mysql_fetch_array($deviceid_query)) {

 //var_dump($deviceid);
     mysql_query("INSERT INTO weekActive (deviceid,date) 
                    VALUES('$deviceid[deviceid]','$everylogdate[date]')") or die(mysql_error());
}

 

  
}


//mysql_close($con);
?>